<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Fam0040f_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getData($id){
    	$this->db->where('asset_id',$id);
    	return $this->db->get('fam_assets')->result();
    }

    public function getLookUp($code)
    {
    	$this->db->where('enable_flag','Y');
    	$this->db->where('lookup_type',$code);
    	return  $this->db->get('app_lookup_values')->result();
    }

    public function getMainCat()
    {
    	$this->db->where('enable_flag','Y');
    	return $this->db->get('fam_categories')->result();
    }

    public function getSubCat($code)
    {
    	$this->db->where('category_code',$code);
    	$this->db->where('enable_flag','Y');
    	return $this->db->get('fam_sub_categories')->result();
    }

    public function getLocation()
    {
    	$this->db->where('enable_flag','Y');
    	return $this->db->get('fam_locations')->result();
    }

    public function getEmployee()
    {
    	return $this->db->get('fam_employees')->result();
    }

    public function insert_data($data){
    	$this->db->query('call fam_assets_ins(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@id,@code,@err,@msg)',$data);
    	$que = $this->db->query("SELECT @err as err,@msg as msg,@code as code,@id as id");
		$msg; $err; $id; $code;
		foreach ($que->result() as $keys) {
			$err = $keys->err;
			$msg = $keys->msg;
			$id = $keys->id;
			$code = $keys->code;
		}
		$arr = array(
			'err' => $err,
			'msg' => $msg,
			'id'  => $id,
			'code' => $code
		);
		return $arr;
    }

    public function update_data($data){
    	$this->db->query('call fam_assets_upd(?,?,?,?,?,?,?,?,?,?,@err,@msg)',$data);
    	$que = $this->db->query("SELECT @err as err,@msg as msg");
		$msg; $err;
		foreach ($que->result() as $keys) {
			$err = $keys->err;
			$msg = $keys->msg;
		}
		$arr = array(
			'err' => $err,
			'msg' => $msg,
		);
		return $arr;
    }

    public function search($code,$mcat,$scat,$assname,$dep,$loc,$assstats,$assdesc,$dcycle)
    {
    	$var = '';
    	$count = 0;
    	if(!empty($code)){
    		$var .= ($count > 0 ? ' AND ' : '')."asset_code LIKE '".$code."' ";
    		$count++;
    	}

    	if(!empty($mcat)){
    		$var .= ($count > 0 ? ' AND ' : '')."main_cat_code LIKE '".$mcat."' ";
    		$count++;
    	}

    	if(!empty($scat)){
    		$var .= ($count > 0 ? ' AND ' : '')."sub_cat_code LIKE '".$scat."' ";
    		$count++;
    	}

    	if(!empty($assname)){
    		$var .= ($count > 0 ? ' AND ' : '')."asset_name LIKE '".$assname."' ";
    		$count++;
    	}

    	if(!empty($dep)){
    		$var .= ($count > 0 ? ' AND ' : '')."department LIKE '".$dep."' ";
    		$count++;
    	}

    	if(!empty($loc)){
    		$var .= ($count > 0 ? ' AND ' : '')."location LIKE '".$loc."' ";
    		$count++;
    	}

    	if(!empty($assstats)){
    		$var .= ($count > 0 ? ' AND ' : '')."status LIKE '".$assstats."' ";
    		$count++;
    	}

    	if(!empty($assdesc)){
    		$var .= ($count > 0 ? ' AND ' : '')."asset_description LIKE '".$assdesc."' ";
    		$count++;
    	}

    	if(!empty($dcycle)){
    		$var .= ($count > 0 ? ' AND ' : '')."depre_cycle LIKE '".$dcycle."' ";
    		$count++;
    	}

    	return $this->db->query('SELECT * FROM fam_assets_v WHERE '.(empty($var) ? '1=0' : $var))->result();

    }
}